﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;

namespace Codes {
    public class Links {
        public static bool add(string Name, string Url, int Order) {
            string sql="insert into Links([Name],[Url],[Addtime],[Order]) values(@Name,@Url,now(),@Order)";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Manager, sql, new OleDbParameter[]{
                new OleDbParameter("@Name", Name),
                new OleDbParameter("@Url", Url),
                new OleDbParameter("@Order", Order)
            })>0;
        }
        public static bool update(string Name, string Url, int Order, int ID) {
            string sql="update Links set Name=@Name,Url=@Url,[Order]=@Order where ID=@ID";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Manager, sql, new OleDbParameter[]{
                new OleDbParameter("@Name", Name),
                new OleDbParameter("@Url", Url),
                new OleDbParameter("@Order", Order),
                new OleDbParameter("@ID", ID)
            })>0;
        }
        public static bool changeOrder(int Order, int ID) {
            string sql="update Links set [Order]=@Order where ID=@ID";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Manager, sql, new OleDbParameter[]{
                new OleDbParameter("@Order", Order),
                new OleDbParameter("@ID", ID)
            })>0;
        }
        public static bool deleteMass(string idList) {
            string sql="delete from Links where ID in("+idList+")";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Manager, sql)>0;
        }

        public static int getListCount() {
            string sql ="select count(1) from links";
            return (int)Codes.AccessHelper.ExecuteScalar(connectionString.Manager, sql);
        }
        public static System.Data.DataTable getList(int pageindex, int pagesize) {
            int notTop=((pageindex-1)*pagesize);
            string sql=string.Empty;
            if(notTop==0) {
                sql="select top "+pagesize+" * from links order by [order] asc";
            } else {
                sql ="select top "+pagesize+" * from links where id not in(select top "+((pageindex-1)*pagesize)+" id from links) order by [order] asc";
            }
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Manager, sql).Tables[0];
        }

        public static System.Data.DataTable getInfo(string id) {
            string sql="select * from links where id="+id;
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Manager, sql).Tables[0];
        }
    }
}